package com.woniu113.junk.site.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.site.dto.AllInboundLogDTO;
import com.woniu113.junk.site.dto.InboundLogDTO;
import com.woniu113.junk.site.mapper.InboundLogMapper;
import com.woniu113.junk.site.mapper.StationInfoMapper;
import com.woniu113.junk.site.model.InboundLog;
import com.woniu113.junk.site.model.StationInfo;
import com.woniu113.junk.site.param.AllInboundLogParam;
import com.woniu113.junk.site.service.InboundLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class InboundLogServiceImpl extends ServiceImpl<InboundLogMapper, InboundLog> implements InboundLogService {

    @Resource
    private InboundLogMapper inboundLogMapper;
    @Resource
    private StationInfoMapper stationInfoMapper;

    @Override
    public AllInboundLogDTO findAll(AllInboundLogParam param, Integer offset) {
        QueryWrapper<InboundLog> queryWrapper = new QueryWrapper<>();
        if (param.getDescription()!=null){
            queryWrapper.eq("description",param.getDescription());
        }
        int size = Math.toIntExact(inboundLogMapper.selectCount(queryWrapper));
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<InboundLog> infos = inboundLogMapper.selectList(queryWrapper);
        ArrayList<InboundLogDTO> inboundLogDTOS = new ArrayList<>();
        for (InboundLog info : infos) {
            StationInfo stationInfo = stationInfoMapper.selectById(info.getStationId());
            InboundLogDTO inboundLogDTO = BeanUtil.toBean(info, InboundLogDTO.class);
            inboundLogDTO.setStationName(stationInfo.getStationName());
            inboundLogDTOS.add(inboundLogDTO);
        }
        AllInboundLogDTO dto = new AllInboundLogDTO();
        dto.setList(inboundLogDTOS);
        dto.setTotal(size);
        return dto;
    }
}
